package com.tpix;

import java.io.IOException;
import static org.easymock.EasyMock.*;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.tpix.photo.PhotoDAO.TestImageService;

public class UploadServlet extends HttpServlet {
	
	BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
	ImagesService imageService = ImagesServiceFactory.getImagesService();
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/**
		 * Testing Code: when done testing comment out:
		 * 
		 * FakeBlobStore fbs = new FakeBlobStore();
		 * Map<String,BlobKey> blobs = fbs.getFakeMap();
		 * 
		 * and uncomment:
		 * Map<String,BlobKey> blobs = blobstoreService.getUploadedBlobs(request);
		 */
		//FakeBlobStore fbs = new FakeBlobStore();
		//Map<String,BlobKey> blobs = fbs.getFakeMap();
		
		
		//get the uploaded image blobkey from the blobstore
		Map<String,BlobKey> blobs = blobstoreService.getUploadedBlobs(request);
		//get the blobkey for the image
		BlobKey blobKey = blobs.get("file");
		System.out.println("In the upload servlet");
		
		//if user clicks upload without selecting a file
		//blobkey in the blobs map is null so redirect to home.jsp
		if(blobKey==null){
		
			System.out.println("blobkey is null");
			
			response.sendRedirect("/upload.jsp");
		}else{
			
		//get the filename from the uploaded blob
		BlobInfo bi =  new BlobInfoFactory().loadBlobInfo(blobKey);
		String filename = bi.getFilename().toLowerCase();
		System.out.println(filename);
		//check if it has the right extension
		//GAE imagesservice accepts jpg, png, gif, webp, bmp, tiff, ico
		if(!filename.endsWith(".jpg")&&!filename.endsWith(".gif")&&
				!filename.endsWith(".jpeg")&&!filename.endsWith("png")&&
				!filename.endsWith(".bmp")&&!filename.endsWith(".tiff")&&
				!filename.endsWith(".ico")&&!filename.endsWith(".webp")){
			//not an image
			response.sendRedirect("/upload.jsp");
		}else{
				
		/** 
		 * Testing code: swap 
		 *    'TestImageService tis = new TestImageService();'
		 *    'String url = tis.getString();'
		 * 
		 * with 
		 * 
		 *    'String url = imageService.getServingUrl(new BlobKey(p.getBlobKey()));' 
		 * to test
		 */

        //TestImageService tis = new TestImageService();
    	//String url = tis.getString();		
		//get the serving url
		String url = imageService.getServingUrl(blobKey,300,false);
		//add url and forward to edit.jsp
		request.setAttribute("url", url);
		// forward the blob-key too
		request.setAttribute("blob-key", blobKey.getKeyString());
		request.getRequestDispatcher("edit.jsp").forward(request, response);
		}
		}
	}
	
	
	/**
	 * A Mock BlobStoreService for testing
	 * @author D
	 *
	 */
	
	public class FakeBlobStore{
		Map<String,BlobKey> fake;
		public FakeBlobStore(){
			fake = new HashMap<String,BlobKey>();
			fake.put("file", new BlobKey("fake_key"));
		}
		
		
		
		public Map<String,BlobKey> getFakeMap(){
			return fake;
		}
	}
	
	/**
	 * TestImageService is just puts out a string
	 * used to mock the getServingURL method
	 * 
	 * @author D
	 *
	 */
	public class TestImageService{
		String test;
		
		public TestImageService(){
			test = "TestString";
		}
		
		public String getString(){
			return test;
		}
	}

}
